﻿using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Collections.Generic;
using System;
using System.Linq;
using HRMSystem.Wpf.WpfWindow;
using HRMSystem.Util;


namespace HRMSystem.Wpf.EmployeePages
{
    /// <summary>
    /// Interaction logic for Employee ContactsInfo.xaml
    /// </summary>
    public partial class ContactsInfo : Page
    {
        private static ContactsInfo _instance;
        public IList<string> ErrorList { get; set; }
        private List<Contact> ContactList { get; set; }
        private Contact itemToUpdate = null;
        private static Mode _mode = Mode.NEW;

        private ContactsInfo()
        {
            InitializeComponent();
            _mode = Mode.NEW;
            ContactList = new List<Contact>();
        }

        public static ContactsInfo Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new ContactsInfo();
                }

                _mode = Mode.NEW;
                return _instance;
            }
        }

        public List<Contact> GetContactDetails()
        {
            return ContactList;
        }

        private Contact GetCurrentContact()
        {
            Contact contact = new Contact();

            contact.EMPLOYEE_ID = Session.NewEmployeeID;

            contact.RELATION = EmpCntRelationTextBox.Text;
            contact.MOBILE = EmpCntMobileTextBox.Text;
            contact.HOME = EmpCntHomeTextBox.Text;
            contact.MOBILE2 = EmpCntOtherTextBox.Text;
            contact.SKYPE = EmpCntSkypeTextBox.Text;
            contact.EMAIL = EmpCntEmailTextBox.Text;

            contact.ISDELETE = false;
            contact.INSERT_DATETIME = DateTime.Now;
            contact.INSERT_USER_ID = Session.LoggedEmployee.ID;
            contact.UPDATE_DATETIME = DateTime.Now;
            contact.UPDATE_USER_ID = Session.LoggedEmployee.ID;

            return contact;
        }

        private void ClearDetails()
        {
            EmpCntRelationTextBox.Text = "";
            EmpCntMobileTextBox.Text = "";
            EmpCntHomeTextBox.Text = "";
            EmpCntOtherTextBox.Text = "";
            EmpCntSkypeTextBox.Text = "";
            EmpCntEmailTextBox.Text = "";

            ContactDetailsAddButton.Width = 75;
            ContactDetailsUpdateButton.Width = 0;
        }

        private void EditContactButton_Click(object sender, RoutedEventArgs e)
        {

            Button button = (Button)sender;
            StackPanel sp = (StackPanel)button.Content;
            Label lbl = sp.Children.OfType<Label>().FirstOrDefault();

            TextBlock tb = sp.Children.OfType<TextBlock>().FirstOrDefault();

            try
            {
                if (lbl.Content.ToString() != "0")
                {
                    itemToUpdate = ContactList.Single(c => c.ID == int.Parse(lbl.Content.ToString()));
                }
                else
                {
                    itemToUpdate = ContactList.Single(c => c.RELATION == tb.Text);
                }

                ContactDetailsAddButton.Width = 0;
                ContactDetailsUpdateButton.Width = 75;

                EmpCntRelationTextBox.Text = itemToUpdate.RELATION;
                EmpCntMobileTextBox.Text = itemToUpdate.MOBILE;
                EmpCntHomeTextBox.Text = itemToUpdate.HOME;
                EmpCntOtherTextBox.Text = itemToUpdate.MOBILE2;
                EmpCntSkypeTextBox.Text = itemToUpdate.SKYPE;
                EmpCntEmailTextBox.Text = itemToUpdate.EMAIL;
            }
            catch (Exception)
            {
                MessageWindow msg = new MessageWindow(Messages.TTL_ERROR, Messages.ERR_DATA);
                msg.ShowDialog();
            }
        }

        private void DeleteContactButton_Click(object sender, RoutedEventArgs e)
        {
            Button button = (Button)sender;
            StackPanel sp = (StackPanel)button.Content;
            Label lbl = sp.Children.OfType<Label>().FirstOrDefault();
            TextBlock tb = sp.Children.OfType<TextBlock>().FirstOrDefault();

            Contact itemToRemove = null;

            try
            {
                if (lbl.Content.ToString() != "0")
                {
                    itemToRemove = ContactList.Single(c => c.ID == int.Parse(lbl.Content.ToString()));
                }
                else
                {
                    itemToRemove = ContactList.Single(c => c.RELATION == tb.Text);
                }

                ContactList.Remove(itemToRemove);
                ContactListView.ItemsSource = ContactList;
                ContactListView.Items.Refresh();
                ClearDetails();
            }
            catch (Exception)
            {
                MessageWindow msg = new MessageWindow(Messages.TTL_ERROR, Messages.ERR_DATA);
                msg.ShowDialog();
            }
        }

        private void ContactDetailsAddButton_Click(object sender, RoutedEventArgs e)
        {
            if (EmpCntRelationTextBox.Text != "")
            {
                Contact contact = GetCurrentContact();

                ContactList.Add(contact);
                ContactListView.ItemsSource = ContactList;
                ContactListView.Items.Refresh();

                ClearDetails();
            }
            else
            {
                MessageWindow msg = new MessageWindow(Messages.TTL_MSG, Messages.MSG_FILL_REQUIRED_INFORMATIONS);
                msg.ShowDialog();
            }
        }

        private void ContactDetailsUpdateButton_Click(object sender, RoutedEventArgs e)
        {
            Contact updatedContact = GetCurrentContact();

            ContactList.Remove(itemToUpdate);
            ContactList.Add(updatedContact);
            ContactListView.ItemsSource = ContactList;
            ContactListView.Items.Refresh();

            ClearDetails();
        }

        private void ContactDetailsCancelButton_Click(object sender, RoutedEventArgs e)
        {
            ClearDetails();
        }
    }
}
